package com.eshop.modules.product.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.product.domain.StoreProductRule;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.modules.product.service.dto.StoreProductRuleDto;
import com.eshop.modules.product.service.dto.StoreProductRuleQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.product.service.StoreProductRuleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "sku规则管理" })
@RestController
@RequestMapping({ "/api/yxStoreProductRule" })
public class StoreProductRuleController
{
    private final StoreProductRuleService storeProductRuleService;
    private final IGenerator generator;
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','yxStoreProductRule:list')")
    public void download(final HttpServletResponse response, final StoreProductRuleQueryCriteria criteria) throws IOException {
        this.storeProductRuleService.download(this.generator.convert(this.storeProductRuleService.queryAll(criteria), (Class)StoreProductRuleDto.class), response);
    }
    
    @GetMapping
    @Log("查询sku规则")
    @ApiOperation("查询sku规则")
    @PreAuthorize("@el.check('admin','yxStoreProductRule:list')")
    public ResponseEntity<Object> getYxStoreProductRules(final StoreProductRuleQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.storeProductRuleService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @PostMapping({ "/save/{id}" })
    @Log("新增/修改sku规则")
    @ApiOperation("新增/修改sku规则")
    @PreAuthorize("hasAnyRole('admin','yxStoreProductRule:add','yxStoreProductRule:edit')")
    public ResponseEntity<Object> create(@Validated @RequestBody final StoreProductRule resources, @PathVariable final Integer id) {
        if (id != null && id > 0) {
            resources.setId(id);
            this.storeProductRuleService.updateById(resources);
        }
        else {
            this.storeProductRuleService.save(resources);
        }
        return new ResponseEntity(HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("删除sku规则")
    @ApiOperation("删除sku规则")
    @PreAuthorize("@el.check('admin','yxStoreProductRule:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Integer[] ids) {
        this.storeProductRuleService.removeByIds((Collection)new ArrayList(Arrays.asList(ids)));
        return new ResponseEntity(HttpStatus.OK);
    }
    
    public StoreProductRuleController(final StoreProductRuleService storeProductRuleService, final IGenerator generator) {
        this.storeProductRuleService = storeProductRuleService;
        this.generator = generator;
    }
}
